package com.tangdi.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.tangdi.ReportFileInfo;
import com.tangdi.Utils;
import com.tangdi.UtilsPrintPDF;
import com.tangdi.Bean.DW_DM_MC;
import com.tangdi.Dao.impl.GongziDWImpl;

public class tfGZ_Transfer_NoticeAction  extends HttpServlet{
	
	private static final long serialVersionUID = 7320075229860488479L;

	private Logger logger = Logger.getLogger(tfGZ_Transfer_NoticeAction.class);
	public tfGZ_Transfer_NoticeAction() {
		super();
	}

	public void destroy() {
		super.destroy();// Just puts "destroy" string in log
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String urlJsp = "tfGZ_Transfer_Notice.jsp";
		String type = request.getParameter("type");
		String year_month = request.getParameter("year_month");

		String folder_Name_tsf = ReportFileInfo.FOLDER_NAME_GZDWTZS.getName();
		String file_Name_tsf = ReportFileInfo.FILE_NAME_GZDWTZS.getName();
		
		logger.info(folder_Name_tsf + " type: " + type);

		HttpSession session=request.getSession();
		if(year_month != null && type.equals("2")){
			//统发工资转账通知书
			List<DW_DM_MC> dw_dm_mcList = new ArrayList<DW_DM_MC>();
			GongziDWImpl gzdwmx = new GongziDWImpl();
			dw_dm_mcList = gzdwmx.getDWInfo(year_month);
			
			session.setAttribute("dw_dm_mcList",dw_dm_mcList);
			session.setAttribute("year_month",year_month);
			session.setAttribute("folder_Name_tsf",folder_Name_tsf);
			session.setAttribute("file_Name_tsf",file_Name_tsf);
			response.sendRedirect(urlJsp);
			
		}else if(type.equals("3")){//打印
			String msg = "success";
			PrintWriter out=response.getWriter();
			String filePathString = request.getParameter("file_paths");
			String[] filePaths = filePathString.substring(0, filePathString.length() - 1).split(",");
			for(int i = 0; i < filePaths.length; i++){
				filePaths[i] = Utils.getFilePath("root_path") + filePaths[i];
				logger.info(filePaths[i]);
			}
			String print_result = "";
			try{
				print_result = UtilsPrintPDF.printPDF(filePaths);
			}catch(Exception e){
				e.printStackTrace();
				msg = "Failure";
				out.print(msg);
				out.flush();
				out.close();
				
			}
			msg = print_result;
			
			logger.info("msg:" + msg);
			out.print(msg);
			out.flush();
			out.close();
			
		}else{
			session.setAttribute("dw_dm_mcList", null);
			session.setAttribute("year_month", null);
			session.setAttribute("folder_Name_tsf", null);
			session.setAttribute("file_Name_tsf", null);
			response.sendRedirect(urlJsp);
			
		}
	}
}
